<?php

/**
 * @Author: king
 * @Date:   2019-05-04 16:53:22
 * @Last Modified by:   king
 * @Last Modified time: 2019-05-30 15:09:44
 */
namespace App\Models\Api;
use App\Exceptions\ApiException;
use App\Models\Api\Base;
use Cache;

class LanmeiCounselorCheck extends Base
{
    protected $table = 'counselor_check';

    /**
     *  保存用户认证信息
     * @param array $data 保存或编辑的数据
     * @param array $id 编辑的id
     * @return bool|false|int
     */
    public function saveAuth($data = [], $id = [])
    {
        if (empty($id)) {
            try {
                $re = $this->insertGetId($data);
            } catch (\Exception $e) {
                 throw new ApiException("认证信息保存失败",config('code.error'));
            }
        } else {
            try {
                $re = $this->save($data, $id);
            } catch (\Exception $e) {
                 throw new ApiException("认证信息保存失败",config('code.error'));
            }
        }

        return $re;
    }


    /**
     * 获取身份证与花名认证信息,用于判断是否重复
     * @param int $uid 用户id
     * @param int $IdNumber 身份证号
     * @param string $NickName 花名
     * @param string $AliNumber 支付宝账号
     * @param int $status 审核状态 1=>未通过审核
     * @return array|bool|false|mixed|\PDOStatement|string|\think\Model
     */
    public function getAuthCheck($uid = 0, $IdNumber = 0, $NickName = '', $AliNumber = '', $status = 0)
    {
        //获取用户未通过审核的认证信息
        if ($status) {
            $where = "status in (1,2) AND uid = $uid";

        }//获取审核信息是否重复
        elseif ($IdNumber && $NickName) {
            $where = "id_number = $IdNumber OR nickname = $NickName OR ali_number = $AliNumber";
        }//获取用户认证信息
        else {
            $where = "uid = $uid";
        }

        try {
            $info = $this->whereRaw($where)->select('uid','truename','telphone','id_number','ali_number','left_pic','right_pic','intro','nickname','status')->orderBy('id', 'DESC')->firstOrFail()->toArray();
        } catch (\Exception $e) {

            $info = null;
        }

        return $info;
    }

}
